#pragma once

#include "../common/singleton.h"
#include <vector>
#include <atomic>
#include <mutex>

class CompassManager {
public:
    DECLARE_SINGLETON_CLASS(CompassManager);

    CompassManager();
    virtual ~CompassManager();

    double getCompassHeading();
    double getCompassLon();
    double getCompassLat();
    void updateCompassHeading(double heading);
    void updateCompassLocation(double lon, double lat);
    void reset();

    void init(double lon, double lat, bool valid);
    void setCompassValid(bool val) { compass_valid_ = val; }
    bool isValid() { return compass_valid_; }
private:
    std::atomic<double> lon_sum_;
    std::atomic<double> lat_sum_;
    std::atomic<unsigned int> state_sum_;
    std::vector<double> headingVec_;

    std::atomic<double> curr_lon_;
    std::atomic<double> curr_lat_;

    std::atomic<bool> compass_valid_;

    std::mutex lock_;
};

typedef pattern::singleton<CompassManager> sln_compass_manager;